<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('设备列表')"/>
    <th:block th:include="include :: bootstrap-editable-css"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>sim卡号：</label>
                            <input type="text" name="sim"/>
                        </li>
                        <li>
                            <label>设备名称：</label>
                            <input type="text" name="name"/>
                        </li>
                        <li>
                            <label>状态：</label>
                            <select name="state">
                                <option value="">所有</option>
                                <option value="0">在线</option>
                                <option value="1">离线</option>
                            </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm"
                               onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm"
                               onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-table-editable-js"/>
<script th:inline="javascript">
    var controlFlag = [[${@permission.hasPermi('equipment:equipment:control')}]];
    var removeFlag = [[${@permission.hasPermi('equipment:equipment:remove')}]];
    var updateRoadFlag = [[${@permission.hasPermi('equipment:equipment:updateRoad')}]];
    var seeingFlag = [[${@permission.hasPermi('visibility:visibility:view')}]];
    var prefix = ctx + "equipment/equipment";
    //所有用户
    var users = [[${users}]];

    $(function () {
        console.log(users)
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "设备",
            onEditableSave: onEditableSave,
            columns: [{
                checkbox: true
            },
                {
                    title: "序号",
                    formatter: function (value, row, index) {
                        return $.table.serialNumber(index);
                    }
                },
                {
                    field: 'id',
                    title: '主键',
                    visible: false
                },
                {
                    field: 'name',
                    title: '设备名称',
                    align: 'center',
                    editable: {
                        type: 'text',
                        title: '修改设备名称',
                        emptytext: "未命名",
                        validate: function (value) {
                            if (value.length > 30) {
                                return '名称不能超过30个字符';
                            } else if (value.length == 0) {
                                return '名称不能为空';
                            }


                        }
                    }
                },
                {
                    field: 'userId',
                    title: '所属用户id',
                    visible: false
                },

                {
                    field: 'userName',
                    title: '所属用户',
                    align: 'center',
                    /*editable: {
                        type: 'select',
                        title: '所属用户',
                        emptytext: "未指定",
                        source: users
                    }*/
                },
                {
                    field: 'sim',
                    title: 'sim卡号',
                    align: 'center'
                },
                {
                    field: 'roadName',
                    title: '所属道路',
                    align: 'center'

                },


                {
                    field: 'workPattern',
                    title: '工作模式',
                    align: 'center',
                    formatter: function (value, row, index) {
                        if (value == '0') {
                            return '<span class="badge badge-danger">关闭</span>';
                        }
                        if (value == '1') {
                            return '<span class="badge badge-primary">手动</span>';
                        }
                        if (value == '2') {
                            return '<span class="badge badge-info">自动</span>';
                        }
                    }
                },
                {
                    field: 'workSubPattern',
                    title: '子工作模式',
                    align: 'center',
                    formatter: function (value, row, index) {
                        if (value == '0') {
                            return '<span class="badge badge-danger">道路强化</span>';
                        }
                        if (value == '1') {
                            return '<span class="badge badge-primary">主动诱导</span>';
                        }
                        if (value == '2') {
                            return '<span class="badge badge-info">防止追尾</span>';
                        }
                    }
                },
                {
                    field: 'timePattern',
                    title: '时间模式',
                    align: 'center',
                    formatter: function (value, row, index) {
                        if (value == '0') {
                            return '<span class="badge badge-danger">全部无效</span>';
                        }
                        if (value == '1') {
                            return '<span class="badge badge-primary">光敏使能</span>';
                        }
                        if (value == '2') {
                            return '<span class="badge badge-info">星历使能</span>';
                        }
                    }
                },

                {
                    field: 'voltage',
                    title: '电池电压(V)',
                    align: 'center',
                    formatter: function (value, row, index) {
                        if (value == null) {
                            return "-";
                        }

                        var str = value.substr(0, 2) + "." + value.substr(2, value.length)
                        return str;


                    }
                },
                {
                    field: 'visibility',
                    title: '能见度(米)',
                    align: 'center'
                },
                {
                    field: 'speedLimit',
                    title: '雷达限速(KM/h)',
                    align: 'center'
                },
                {
                    field: 'updateTime',
                    title: '最后更新时间',
                    align: 'center'
                },
                {
                    field: 'state',
                    title: '状态',
                    align: 'center',
                    formatter: function (value, row, index) {
                        if (value == '0') {
                            return '<span class="badge badge-primary">在线</span>';
                        } else if (value == '1') {
                            return '<span class="badge badge-danger">离线</span>';
                        }
                    }
                },

                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + controlFlag
                            + '" href="javascript:void(0)" onclick="$.operate.editTab1(\'' + row.id
                            + '\')"><i class="fa fa-dashboard"></i>设备控制</a> ');
                        actions.push('<a class="btn btn-info btn-xs ' + true
                            + '" href="javascript:void(0)" onclick="lookOrders(\'' + row.sim
                            + '\')"><i class="fa fa-edit"></i>命令记录</a> ');

                        actions.push('<a class="btn btn-info btn-xs ' + updateRoadFlag
                            + '" href="javascript:void(0)" onclick="updateRoad(\'' + row.id
                            + '\')"><i class="fa fa-edit"></i>更改道路</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag
                            + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id
                            + '\')"><i class="fa fa-remove"></i>删除</a> ');
                        actions.push('<a class="btn btn-primary btn-xs ' + seeingFlag
                            + '" href="javascript:void(0)" onclick="querySeeing(\'' + row.sim
                            + '\')"><i class="fa fa-line-chart"></i>能见度</a> ');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    //能见度
    function querySeeing(sim) {
        window.location.href = ctx + "visibility/visibility/query/" + sim;

    }

    //修改设备名称
    function onEditableSave(field, row, oldValue, $el) {
        if (field == "name") {
            let id = row.id;
            let name = row[field];
            var reg = /[~#^$@%&!?%*]/gi;
            if (reg.test(name)) {
                $.modal.alertWarning("名称含有非法字符，请重新填写")
                return;

            }
            $.operate.save(ctx + "equipment/equipment/editEquimentName/" + id + "/" + name);
        }
        if (field == "userName") {
            let id = row.id;
            let userName = row[field];
            $.operate.save(ctx + "equipment/equipment/editEquimentUserName/" + id + "/" + userName);
        }


    }

    //更改道路
    function updateRoad(id) {
        let url = ctx + '/equipment/equipment' + '/updateRoad/' + id;
        $.modal.open("更改道路", url, '600', '500');
    }

    //标记客户
    function markUser(id) {
        const url = ctx + '/equipment/equipment' + '/updateRoad/' + id;
        $.modal.open("更改道路", url, '600', '500');


    }

    function lookOrders(sim) {
        const url = prefix + '/lookOrders/' + sim;
        $.modal.open("命令记录", url);

    }
</script>
</body>
</html>